package com.zhaolixiang.slidetabview

import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Color
import android.os.Handler
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.widget.HorizontalScrollView
import android.widget.LinearLayout
import android.widget.TextView
import java.util.jar.Attributes
import com.zhaolixiang.slidetabview.SlideTabLinearLayout.ScrollType




/**
 * Created by Administrator on 2018/1/3.
 */
class SlideTabLinearLayout :HorizontalScrollView {
    private var leftAndRightMinWidth=0
    //滑动的x坐标值
    private var perviousX=0f

    /**
     * 滚动状态:
     * IDLE=滚动停止
     * TOUCH_SCROLL=手指拖动滚动
     * FLING=滚动
     */
    enum class ScrollType {
        IDLE, TOUCH_SCROLL, FLING
    };
    /**
     * 滚动方向：direction
     */
    enum class Direction{
        LEFT,RIGHT,NON
    }
    //初始化，未设置
    private var direction=Direction.NON

    /**
     * 当前滚动状态
     */
    private var scrollType = ScrollType.IDLE
    private var mScrollViewListener: ScrollViewListener?=null
    private lateinit var mHandler: Handler
    /**
     * 停止滚动后记录当前滚动的距离
     */
    private var currentX = -9999999
    val width1=288;
    val width2=288;
    val width3=288;
    val width4=288;
    interface ScrollViewListener {
        fun onScrollChanged(scrollType: ScrollType)
    }

    constructor(context: Context):super(context)
    constructor(context: Context,attrs:AttributeSet):super(context,attrs)
    constructor(context: Context,attrs: AttributeSet,defStyleAttr:Int):super(context,attrs,defStyleAttr)

    init {
        mHandler = Handler();
        mScrollViewListener=object :ScrollViewListener{
            override fun onScrollChanged(scrollType: ScrollType) {
                if(scrollType==ScrollType.IDLE){
                    Log.e("滑动亭子","最终效果"+currentX)
                    //judge(currentX)
                    //changeSize(currentX)
                }
            }
        }
    }
    private fun mySmoothScrollTo(x:Int,y:Int){
        smoothScrollTo(x,y)
    }
    //设置左右两边边至少剩余的宽度
    public fun setMinWidth(minWidth:Int){
        //1152
        mySmoothScrollTo(minWidth,0)
        //1312
        Log.e("设置左边最少距离",""+minWidth+"---"+scaleX)
        leftAndRightMinWidth=minWidth

    }


    override fun onScrollChanged(x: Int, y: Int, oldX: Int, oldY: Int) {

        Log.e("滑动了","onScrollChanged$x---$oldX")
        if(leftAndRightMinWidth>0){
            Log.e("滑动了2","onScrollChanged$x---$oldX")
            //只有当初始化后才有效果
            if(x<leftAndRightMinWidth){
                Log.e("滑动了3","onScrollChanged$x---$oldX")
                //如果向左滑动了，就重新添加一组数据到左边
              //  leftAdd()
                if(oldX==0){
                    mySmoothScrollTo(leftAndRightMinWidth,0)
                }else{
                    mySmoothScrollTo(x+leftAndRightMinWidth,0)
                }

            }else if(x>2*leftAndRightMinWidth){
                Log.e("滑动了4","onScrollChanged$x---$oldX")
                //这里会出现滑动卡顿现象
                mySmoothScrollTo(x-leftAndRightMinWidth,0)
            }
        }
        Log.e("滑动了5","onScrollChanged$x---$oldX")
        super.onScrollChanged(x, y, oldX, oldY)
        mHandler.post(scrollRunnable);
    }

    private fun judge(scrollX2:Int){
        var scrollX=scrollX2
        //需要转换一下：
        if(scrollX<leftAndRightMinWidth){
            scrollX=scrollX+leftAndRightMinWidth
        }else if(x>2*leftAndRightMinWidth){
            scrollX=scrollX-leftAndRightMinWidth
        }
        val cha =scrollX-leftAndRightMinWidth
        //判断所属区间
        Log.e("差值","leftAndRightMinWidth:$leftAndRightMinWidth---"+cha)
        if(cha<width1/2){
            mySmoothScrollTo(leftAndRightMinWidth,0)
        }else if(cha<width1){
            mySmoothScrollTo(leftAndRightMinWidth+width1,0)
        }else if(cha<width1+width2/2){
            mySmoothScrollTo(leftAndRightMinWidth+width1,0)
        }else if(cha<width1+width2){
            mySmoothScrollTo(leftAndRightMinWidth+width1+width2,0)
        }else if(cha<width1+width2+width3/2){
            mySmoothScrollTo(leftAndRightMinWidth+width1+width2,0)
        }else if(cha<width1+width2+width3){
            mySmoothScrollTo(leftAndRightMinWidth+width1+width2+width3,0)
        }else if(cha<width1+width2+width3+width4/2){
            mySmoothScrollTo(leftAndRightMinWidth+width1+width2+width3,0)
        }else if(cha<width1+width2+width3+width4){
            smoothScrollTo(leftAndRightMinWidth,0)
        }


    }

    /**
     * 滚动监听runnable
     */
    private val scrollRunnable = object : Runnable {
        override fun run() {
            if (scrollX == currentX) {
                //滚动停止,取消监听线程
                scrollType = ScrollType.IDLE
                if (mScrollViewListener != null) {
                    mScrollViewListener!!.onScrollChanged(scrollType)
                }
                mHandler.removeCallbacks(this)
                return
            } else {
                Log.e("手指离开屏幕,但是view还在滚动","手指离开屏幕,但是view还在滚动"+currentX)
                //手指离开屏幕,但是view还在滚动
                scrollType = ScrollType.FLING
                if (mScrollViewListener != null) {
                    mScrollViewListener!!.onScrollChanged(scrollType)
                }
            }
            currentX = scrollX
            Log.e("停止滚动后记录当前滚动的距离",""+currentX)
            //滚动监听间隔:milliseconds
            mHandler.postDelayed(this, 50)
        }
    }

    override fun onTouchEvent(ev: MotionEvent): Boolean {

        when(ev.action){
            MotionEvent.ACTION_DOWN->{
                //按下开始滑动
                perviousX=ev.x
            }
            MotionEvent.ACTION_MOVE->{
                //触摸移动中
                if(ev.x>perviousX){
                    //想右移动
                    direction=Direction.RIGHT
                }else if(ev.x<perviousX){
                    //向左移动
                    direction=Direction.LEFT
                }else{
                    direction=Direction.NON
                }
                this.scrollType=ScrollType.TOUCH_SCROLL
                if (mScrollViewListener != null) {
                    mScrollViewListener!!.onScrollChanged(scrollType);
                }
                mHandler.removeCallbacks(scrollRunnable);
            }
            MotionEvent.ACTION_UP->{
                //手指离开时
                mHandler.post(scrollRunnable);
            }
        }
        return super.onTouchEvent(ev)
    }
    //根据坐标获取坐标值
    private fun changeSize(scrollX2:Int){
        var scrollX=scrollX2
        Log.e("changeSize","scrollX$scrollX")
        //需要转换一下：
        if(scrollX<leftAndRightMinWidth){
            scrollX=scrollX+leftAndRightMinWidth
        }else if(x>2*leftAndRightMinWidth){
            scrollX=scrollX-leftAndRightMinWidth
        }

        val cha =scrollX-leftAndRightMinWidth
        //判断所属区间
        Log.e("差值","leftAndRightMinWidth:$leftAndRightMinWidth---"+cha)
        if(cha<width1/2){
            getNowId(4)
        }else if(cha<width1){
            getNowId(5)
        }else if(cha<width1+width2/2){
            getNowId(5)
        }else if(cha<width1+width2){
            getNowId(6)
        }else if(cha<width1+width2+width3/2){
            getNowId(6)
        }else if(cha<width1+width2+width3){
            getNowId(7)
        }else if(cha<width1+width2+width3+width4/2){
            getNowId(7)
        }else if(cha<width1+width2+width3+width4){
            getNowId(4)
        }
    }
    //设置最左边控件样式
    private fun getNowId(ids:Int){

        //设置其他颜色为黑色
        for(itemId in 0.. (this.getChildAt(0) as LinearLayout).childCount-1){
            val item=(this.getChildAt(0) as LinearLayout).getChildAt(itemId) as TextView
            if(itemId%4!=ids%4){
                item.setTextColor(Color.parseColor("#333333"))
            }else{
                item.setTextColor(Color.parseColor("#448ced"))
            }
        }
    }


    private fun move(moveX:Float){
        Log.e("移动距离", "moveX：$moveX")
        val objectAnimatorX=ObjectAnimator.ofFloat(this,"translationX",moveX)
        objectAnimatorX.start()

    }

}